﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Timers;

namespace JQGJJ
{
    public partial class SerialPortSetDlg : Form
    {
        public static SerialPort sp = new SerialPort();
        private static bool noDefault = false;
        private static bool isSerialPortConnected = false;
        private static bool firstIsClear = true;
        public static object _lock = new object();
        //Thread th = new Thread(ThreadChild);
        
        public static bool GetSerialPortConnected()
        {
            return isSerialPortConnected;
        }
        public SerialPortSetDlg()
        {
            InitializeComponent();
        }

        private void SerialPortSetDlg_Load(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();
            if (portNames.Length >0)
            {
                if (noDefault)
                    return;

                if (!firstIsClear)
                    return;

                foreach (string name in portNames)
                {
                    this.cbPortName.Items.Add(name);
                }

                this.cbPortName.SelectedIndex = 0;
                this.cbBTL.SelectedIndex = 9;
                this.cbData.SelectedIndex = 3;
                this.cbStop.SelectedIndex = 1;
                this.cbJy.SelectedIndex = 0;
                this.btnOK.Enabled = true;
                if (sp.IsOpen)
                    return;
                firstIsClear = false;
            }
            else
            {
                firstIsClear = true;
                this.cbPortName.Items.Clear();
                this.btnOK.Enabled = false;
            }
        }

        public void setComboBoxState(bool state)
        {
            this.cbBTL.Enabled = state;
            this.cbPortName.Enabled = state;
            this.cbJy.Enabled = state;
            this.cbData.Enabled = state;
            this.cbStop.Enabled = state;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            lock(_lock)
            {
                if (string.Compare(this.btnOK.Text, "打开") == 0)
                {
                    sp.PortName = this.cbPortName.Text;
                    sp.BaudRate = Convert.ToInt32(this.cbBTL.Text);
                    sp.DataBits = Convert.ToInt32(this.cbData.Text);
                    sp.StopBits = (StopBits)Convert.ToInt32(this.cbStop.Text);
                    sp.Parity = (Parity)this.cbJy.SelectedIndex;
                    SendData.setSerialPort(sp);
                    SendData.stopSate = false;
                    if (sp.IsOpen)
                    {
                        return;
                    }
                    sp.Open();
                    if (sp.IsOpen)
                    {
                        setComboBoxState(false);
                        isSerialPortConnected = true;
                        this.btnOK.Text = "关闭";
                    }
                    else
                        MessageBox.Show("串口打开失败！");
                }
                else
                {
                    SendData.stopSate = true;
                    Thread.Sleep(1);
                    if (sp.IsOpen)
                    {
                        try
                        {
                            this.btnOK.Text = "打开";
                            setComboBoxState(true);
                            isSerialPortConnected = false;
                            sp.Close();
                            sp.Dispose();
                        }
                        catch
                        {
                            MessageBox.Show("串口关闭失败！");
                        }
                    }
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();
            if (portNames.Length > 0)
                noDefault = true;
            this.Hide();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();
            if (portNames.Length > 0)
                noDefault = true;
            SendData.stopSate = true;
            Thread.Sleep(1);
            if (sp.IsOpen)
            {
                try
                {
                    sp.Close();
                    sp.Dispose();
                }
                catch
                {

                }
            }
                
        }

        private void SerialPortSetDlg_FormClosed(object sender, FormClosedEventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();
            if (portNames.Length > 0)
                noDefault = true;
            SendData.stopSate = true;
            Thread.Sleep(1);
            if (sp.IsOpen)
            {
                try
                {
                    sp.Close();
                    sp.Dispose();
                }
                catch
                {

                }
            }
        }
    }
}
